package com.zhiqianbao.utils;

import android.text.TextUtils;

import java.security.MessageDigest;
import java.util.Map;
import java.util.TreeMap;

import cn.trinea.android.common.util.StringUtils;

/**
 * Created by hp on 2017/10/11.
 */
public class SignConfigUtils {
    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "a", "b", "c", "d", "e", "f"};

    public static String getSignParams(Map<String, String> params) {
        Map<String, String> treeMap = new TreeMap<>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (entry != null) {
                if (!TextUtils.isEmpty(entry.getValue())) {
                    treeMap.put(entry.getKey(), entry.getValue());
                }
            }
        }
        StringBuilder buffers = new StringBuilder();
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            if (entry != null) {
                buffers.append(entry.getKey()).append("=")
                        .append(entry.getValue()).append("&");
            }
        }
        String descStr = buffers.toString();
        String result = "";
        if (descStr.length() > 0) {
            result = descStr.substring(0, descStr.length() - 1);
        }
        result = MD5Encode(result + getSignKey()).substring(8, 24) + MD5Encode(getSignKey()).substring(8, 24);
        return result.toUpperCase();
    }

    public static String getSignTreeParams(Map<String, String> treeMap) {
        if (treeMap == null) {
            return "";
        }
        StringBuffer buffers = new StringBuffer();
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            if (entry != null) {
                if (!TextUtils.isEmpty(entry.getValue())) {
                    buffers.append(entry.getKey()).append("=")
                            .append(entry.getValue()).append("&");
                }
            }
        }
        String descStr = buffers.toString();
        String result = "";
        if (descStr.length() > 0) {
            result = descStr.substring(0, descStr.length() - 1);
        }
        result = MD5Encode(result + getSignKey()).substring(8, 24) + MD5Encode(getSignKey()).substring(8, 24);
        return result.toUpperCase();
    }

    public static String getSign(Map<String, String> params) {
        Map<String, String> sortMap = new TreeMap<String, String>();
        sortMap.putAll(params);
        // 以k1=v1&k2=v2...方式拼接参数
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, String> s : sortMap.entrySet()) {
            String k = s.getKey();
            String v = s.getValue();
            if (StringUtils.isBlank(v)) {// 过滤空值
                continue;
            }
            builder.append(k).append("=").append(v).append("&");
        }
        if (!sortMap.isEmpty()) {
            builder.deleteCharAt(builder.length() - 1);
        }
        return (MD5Encode(builder.toString()+ getSignKey()).substring(8,24)
                +MD5Encode( getSignKey()).substring(8, 24)).toUpperCase();
    }

    /**
     * 获取AppKey part1
     */
    public static String getSignKey() {
        return "mimidai123";
    }

    /**
     * 转换字节数组为16进制字串
     *
     * @param b 字节数组
     * @return 16进制字串
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuilder resultSb = new StringBuilder();
        for (byte aB : b) {
            resultSb.append(byteToHexString(aB));
        }
        return resultSb.toString();
    }

    /**
     * 转换byte到16进制
     *
     * @param b 要转换的byte
     * @return 16进制格式
     */
    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    /**
     * MD5编码
     *
     * @param origin 原始字符串
     * @return 经过MD5加密之后的结果
     */
    public static String MD5Encode(String origin) {
        String resultString = null;
        try {
            resultString = origin;
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(resultString.getBytes("UTF-8"));
            resultString = byteArrayToHexString(md.digest());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

}
